﻿Imports System.Data.OleDb

Public Class frmCapNhatGiaTheoNhom
    Public NhomId As Integer = -1
    Public TenNhom As String = ""
    Private Sub frmCapNhatGiaTheoNhom_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            Me.BackColor = Color.FromArgb(194, 217, 247)
            txtGiaCu.Text = "0"
            txtGiaMoi.Text = "0"
            If NhomId > 0 Then
                lblNhomSanPham.Text = TenNhom
                SanPhamTheoNhom_Load(NhomId)
            End If
        Catch ex As Exception

        End Try
    End Sub
#Region "Load Data"
    Protected Sub SanPhamTheoNhom_Load(ByVal NhomId As Integer)
        Try
            Dim dtNhom As New DataTable
            Dim dbConn As New OleDb.OleDbConnection
            Dim dataAccess As New DataAccess
            Dim adapter As OleDb.OleDbDataAdapter
            Dim dbCommand As OleDb.OleDbCommand
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            dbCommand = New OleDbCommand("spSanPhamNhom_GetAll", dbConn)
            dbCommand.CommandType = CommandType.StoredProcedure
            dbCommand.Parameters.Add("@NhomId", OleDbType.Integer).Value = NhomId
            adapter = New OleDb.OleDbDataAdapter(dbCommand)
            adapter.Fill(dtNhom)
            dbConn.Close()
            '
            Dim GiaTheoNhom As DataTable = Table_MultiColumn()
            Dim newRow As DataRow
            For i As Integer = 0 To dtNhom.Rows.Count - 1
                newRow = GiaTheoNhom.NewRow
                If i < dtNhom.Rows.Count Then
                    newRow.Item("MaSanPham1") = dtNhom.Rows(i)("MaSanPham")
                    newRow.Item("DonGia") = dtNhom.Rows(i)("DonGia")
                Else
                    newRow.Item("MaSanPham1") = ""
                    newRow.Item("DonGia") = ""
                End If
                If i + 1 < dtNhom.Rows.Count Then
                    newRow.Item("MaSanPham2") = dtNhom.Rows(i + 1)("MaSanPham")
                Else
                    newRow.Item("MaSanPham2") = ""
                End If
                If i + 2 < dtNhom.Rows.Count Then
                    newRow.Item("MaSanPham3") = dtNhom.Rows(i + 2)("MaSanPham")
                Else
                    newRow.Item("MaSanPham3") = ""
                End If
                If i + 3 < dtNhom.Rows.Count Then
                    newRow.Item("MaSanPham4") = dtNhom.Rows(i + 3)("MaSanPham")
                Else
                    newRow.Item("MaSanPham4") = ""
                End If
                If i + 4 < dtNhom.Rows.Count Then
                    newRow.Item("MaSanPham5") = dtNhom.Rows(i + 4)("MaSanPham")
                Else
                    newRow.Item("MaSanPham5") = ""
                End If
                i = i + 4
                GiaTheoNhom.Rows.Add(newRow)
            Next
            'bind data
            dgvSanPhamTheoNhom.Rows.Clear()
            dgvSanPhamTheoNhom.ColumnsCount = 6
            'tạo header 
            dgvSanPhamTheoNhom.FixedRows = 1
            dgvSanPhamTheoNhom.Rows.Insert(0)
            dgvSanPhamTheoNhom(0, 0) = New SourceGrid.Cells.ColumnHeader("MÃ SẢN PHẨM")
            dgvSanPhamTheoNhom(0, 0).Column.Width = 750
            dgvSanPhamTheoNhom(0, 0).ColumnSpan = 5
            dgvSanPhamTheoNhom(0, 5) = New SourceGrid.Cells.ColumnHeader("ĐƠN GIÁ")
            dgvSanPhamTheoNhom(0, 5).Column.Width = 150
            dgvSanPhamTheoNhom.Rows.Insert(1)
            Dim rowCOLSPAN As New SourceGrid.Cells.Views.Cell
            rowCOLSPAN.BackColor = Color.FromArgb(192, 255, 192)
            dgvSanPhamTheoNhom(1, 0) = New SourceGrid.Cells.Cell(TenNhom)
            dgvSanPhamTheoNhom(1, 0).Column.Width = 750
            dgvSanPhamTheoNhom(1, 0).ColumnSpan = 6
            dgvSanPhamTheoNhom(1, 0).View = rowCOLSPAN
            For i As Integer = 0 To GiaTheoNhom.Rows.Count - 1
                dgvSanPhamTheoNhom.Rows.Insert(i + 2)
                dgvSanPhamTheoNhom(i + 2, 0) = New SourceGrid.Cells.Cell(GiaTheoNhom.Rows(i)("MaSanPham1"))
                dgvSanPhamTheoNhom(i + 2, 0).Column.Width = 150
                dgvSanPhamTheoNhom(i + 2, 1) = New SourceGrid.Cells.Cell(GiaTheoNhom.Rows(i)("MaSanPham2"))
                dgvSanPhamTheoNhom(i + 2, 1).Column.Width = 150
                dgvSanPhamTheoNhom(i + 2, 2) = New SourceGrid.Cells.Cell(GiaTheoNhom.Rows(i)("MaSanPham3"))
                dgvSanPhamTheoNhom(i + 2, 2).Column.Width = 150
                dgvSanPhamTheoNhom(i + 2, 3) = New SourceGrid.Cells.Cell(GiaTheoNhom.Rows(i)("MaSanPham4"))
                dgvSanPhamTheoNhom(i + 2, 3).Column.Width = 150
                dgvSanPhamTheoNhom(i + 2, 4) = New SourceGrid.Cells.Cell(GiaTheoNhom.Rows(i)("MaSanPham5"))
                dgvSanPhamTheoNhom(i + 2, 4).Column.Width = 150
            Next
            Dim FontBold As New Font(dgvSanPhamTheoNhom.Font, FontStyle.Bold)
            Dim colRowSPAN As New SourceGrid.Cells.Views.Cell
            colRowSPAN.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter
            colRowSPAN.Font = FontBold
            dgvSanPhamTheoNhom(2, 5) = New SourceGrid.Cells.Cell(Format(Double.Parse(GiaTheoNhom.Rows(0)("DonGia")), "N02"))
            dgvSanPhamTheoNhom(2, 5).Column.Width = 150
            dgvSanPhamTheoNhom(2, 5).RowSpan = GiaTheoNhom.Rows.Count
            dgvSanPhamTheoNhom(2, 5).View = colRowSPAN
            txtGiaCu.Text = GiaTheoNhom.Rows(0)("DonGia")
        Catch ex As Exception

        End Try
    End Sub
    Protected Function Table_MultiColumn() As DataTable
        Dim dtTable As DataTable = New DataTable()
        Try
            Dim MaSanPham1 As DataColumn = New DataColumn("MaSanPham1")
            MaSanPham1.DataType = System.Type.GetType("System.String")
            MaSanPham1.DefaultValue = ""
            dtTable.Columns.Add(MaSanPham1)

            Dim MaSanPham2 As DataColumn = New DataColumn("MaSanPham2")
            MaSanPham2.DataType = System.Type.GetType("System.String")
            MaSanPham2.DefaultValue = ""
            dtTable.Columns.Add(MaSanPham2)

            Dim MaSanPham3 As DataColumn = New DataColumn("MaSanPham3")
            MaSanPham3.DataType = System.Type.GetType("System.String")
            MaSanPham3.DefaultValue = ""
            dtTable.Columns.Add(MaSanPham3)

            Dim MaSanPham4 As DataColumn = New DataColumn("MaSanPham4")
            MaSanPham4.DataType = System.Type.GetType("System.String")
            MaSanPham4.DefaultValue = ""
            dtTable.Columns.Add(MaSanPham4)

            Dim MaSanPham5 As DataColumn = New DataColumn("MaSanPham5")
            MaSanPham5.DataType = System.Type.GetType("System.String")
            MaSanPham5.DefaultValue = ""
            dtTable.Columns.Add(MaSanPham5)

            Dim DonGia As DataColumn = New DataColumn("DonGia")
            DonGia.DataType = System.Type.GetType("System.String")
            DonGia.DefaultValue = ""
            dtTable.Columns.Add(DonGia)

            Return dtTable
        Catch ex As Exception
            Throw
        End Try
    End Function
#End Region

    Private Sub cmdCapNhat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCapNhat.Click
        Try
            Dim dbConn As New OleDbConnection
            Dim dataAccess As New DataAccess
            Dim dbCommand As OleDbCommand
            
            Dim dlgRes As DialogResult
            dlgRes = MessageBox.Show( _
                  "Bạn muốn cập nhật đơn giá " & txtGiaMoi.Value & " cho nhóm sản phẩm " & lblNhomSanPham.Text, _
                  "Cập nhật đơn giá", _
            MessageBoxButtons.OKCancel, _
                  MessageBoxIcon.Question)
            If dlgRes = DialogResult.OK Then
                dataAccess.ConnectDatabase_DBAccess(dbConn)
                dbCommand = New OleDbCommand("spSanPham_CapNhatGia", dbConn)
                dbCommand.CommandType = CommandType.StoredProcedure
                dbCommand.Parameters.Add("@NhomId", OleDbType.Integer).Value = NhomId
                dbCommand.Parameters.Add("@DonGia", OleDbType.Integer).Value = txtGiaMoi.Value
                dbCommand.Parameters.Add("@GhiChu", OleDbType.VarWChar).Value = txtGhiChu.Text.Trim()
                dbCommand.Parameters.Add("@NgaySua", OleDbType.VarWChar).Value = NguoiTao()
                dbCommand.Parameters.Add("@NguoiSua", OleDbType.VarWChar).Value = NgayTao()
                dbCommand.ExecuteNonQuery()
                dbConn.Close()
                txtGiaCu.Text = "0"
                txtGiaMoi.Text = "0"
                TenNhom = ""
                NhomId = -1
                Me.Close()
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub cmdHuyBo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdHuyBo.Click
        Try
            txtGiaCu.Text = "0"
            txtGiaMoi.Text = "0"
            TenNhom = ""
            NhomId = -1
            Me.Close()
        Catch ex As Exception

        End Try
    End Sub
End Class